package it.david.javassist

import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import org.apache.commons.io.FileUtils
import org.gradle.api.Project
import org.apache.commons.codec.digest.DigestUtils

public class JavassistTransform extends Transform {

    private Project mProject

    public JavassistTransform(Project project) {
        this.mProject = project
    }

    @Override
    String getName() {
        return 'javassistTransform'
    }

    /**
     * 指定输入的类型，通过这里设定，可以指定我们要处理的文件类型
     * 这样确保其他类型的文件不会传入
     */
    @Override
    Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS
    }

    /**
     * 指定Transform的作用范围
     * @return
     */
    @Override
    Set<QualifiedContent.Scope> getScopes() {
        return TransformManager.SCOPE_FULL_PROJECT
    }

    @Override
    boolean isIncremental() {
        return false
    }

    @Override
    void transform(Context context, Collection<TransformInput> inputs,
                   Collection<TransformInput> referencedInputs,
                   TransformOutputProvider outputProvider,
                   boolean isIncremental)
            throws IOException, TransformException, InterruptedException {
        inputs.each { TransformInput input ->
            //对类型文为文件夹的input进行遍历
            input.directoryInputs.each { DirectoryInput directoryInput ->
                //文件夹里面包含的是我们手写的类以及R.class、BuildConfig.class以及R$XXX.class等
                JavassistInject.injectDir(directoryInput.file.absolutePath, "generaladapter/dengwei/it/javassistdemo")
                //获取output目录
                def dest = outputProvider.getContentLocation(directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
                FileUtils.copyDirectory(directoryInput.file, dest)
            }
            input.jarInputs.each { JarInput jarInput ->
                //jar 文件一般是第三方依赖库jar文件
                //重命名输出文件（同目录copyFile会冲突）
                def jarName = jarInput.name
                def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
                if (jarName.endsWith(".jar")) {
                    jarName = jarName.substring(0, jarName.length() - 4) //因为.jar 刚好是4个长度
                }
                File copyJarFile = jarInput.file
                def libJar = jarInput.file.getAbsoluteFile().name

                if (libJar.contains("lib.jar")){
                    JarZipUtils.unZipJar(jarInput.file.getAbsolutePath(), "./tmp/lib")
                    JavassistInject.injectDir("./tmp/lib", "generaladapter/dengwei")
                    JarZipUtils.zipJar("./tmp/lib", "./tmp/lib.jar")
                    copyJarFile = new File("./tmp/lib.jar")
                }

                def dest = outputProvider.getContentLocation(jarName + md5Name, jarInput.contentTypes, jarInput.scopes, Format.JAR)
                if (copyJarFile.absolutePath.contains("support-v4")) {
                    FileUtils.copyFile(jarInput.file, dest)
                }
            }

        }
    }

    @Override
    void transform(TransformInvocation transformInvocation)
            throws TransformException, InterruptedException, IOException {
        super.transform(transformInvocation)
//        this.transform(transformInvocation.getContext(),
//                transformInvocation.getInputs(),
//                transformInvocation.getReferencedInputs(),
//                transformInvocation.getOutputProvider(),
//                transformInvocation.isIncremental());
//        Collection<TransformInput> inputs = transformInvocation.getInputs()
//        TransformOutputProvider outputProvider = transformInvocation.getOutputProvider()
//        inputs.each {TransformInput input ->
//            //对类型文为文件夹的input进行遍历
//            input.directoryInputs.each {DirectoryInput directoryInput ->
//                //文件夹里面包含的是我们手写的类以及R.class、BuildConfig.class以及R$XXX.class等
//                JavassistInject.injectDir(directoryInput.file.absolutePath, "generaladapter\\dengwei\\it\\javassistdemo")
//                //获取output目录
//                def dest = outputProvider.getContentLocation(directoryInput.name, directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
//                FileUtils.copyDirectory(directoryInput.file, dest)
//            }
//            input.jarInputs.each {JarInput jarInput ->
//                //jar 文件一般是第三方依赖库jar文件
//                //重命名输出文件（同目录copyFile会冲突）
//                def jarName = jarInput.name
//                def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
//                if (jarName.endsWith(".jar")) {
//                    jarName = jarName.substring(0, jarName.length() - 4) //因为.jar 刚好是4个长度
//                }
//                def dest = outputProvider.getContentLocation(jarName + md5Name, jarInput.contentTypes, jarInput.scopes, Format.JAR)
//                FileUtils.copyDirectory(jarInput.file, dest)
//            }
//        }
    }
}